﻿using UnityEngine;
using System.Collections;  
 
public class BattleCamera : MonoBehaviour {  
  
    //最大最小z坐标
    public float minZ = 47f;
    public float maxZ = 78f;

    //最大最小x坐标
    public float minX = -9.2f;
    public float maxX = 10.8f;

    //最大摄像机视野
    public float maxFov = 50f;
    
    //最下摄像机视野
    public float minFov = 30f;

    //多点触摸参数
    private Vector2 _lastTouchPos1 = Vector2.zero;
    private Vector2 _lastTouchPos2 = Vector2.zero;

    private float _lastTwoDis = 0.0f;
    private Vector2 _lastTwoDir;


    //记录手指触碰的位置
    Vector2 m_screenPos = new Vector2(); 
    
    void Start () {

        //开启多点触碰
        Input.multiTouchEnabled = true;
    }  
      
      
    void Update () {
        if (Input.touchCount <= 0)
        {
            return;
        }
        //单点触碰移动摄像机
        if (Input.touchCount == 1)
        {
            if (Input.touches[0].phase == TouchPhase.Began)
            {
                //单点触碰移动摄像机
                m_screenPos = Input.touches[0].position;
            }
            //手指在屏幕上移动，移动摄像机
            if (Input.touches[0].phase == TouchPhase.Moved)
            {
                Vector3 offset = new Vector3(Input.touches[0].deltaPosition.x * Time.deltaTime, 0, Input.touches[0].deltaPosition.y * Time.deltaTime);
                if (transform.position.x + offset.x >= maxX)
                {
                    offset.x = maxX - transform.position.x;
                }
                if (transform.position.x + offset.x <= minX)
                {
                    offset.x = minX - transform.position.x;
                }
                if (transform.position.z + offset.z >= maxZ)
                {
                    offset.z = maxZ - transform.position.z;
                }
                if (transform.position.y + offset.y <= maxZ)
                {
                    offset.z = maxZ - transform.position.z;
                }
                transform.Translate(offset);
            }
        }
        //多点触碰
        else if (Input.touchCount > 1)
        {
            Touch t1 = Input.GetTouch(0);
            Touch t2 = Input.GetTouch(1);

            if (_lastTouchPos2 != Vector2.zero)
            {
                Vector2 touch1Delta = t1.position - _lastTouchPos1;
                Vector2 touch2Delta = t2.position - _lastTouchPos2;

                float curDis = Vector2.Distance(t1.position - t2.position, new Vector3(0, 1, 0));
                Vector2 curDir = t1.position - t2.position;

                if (_lastTwoDir != Vector2.zero)
                {
                    float offDis = 0.0f;
                    if (_lastTwoDis != 0)
                    {
                        if (curDis > _lastTwoDis)
                        {
                            offDis = -Vector2.Distance(touch1Delta, touch2Delta);
                        }
                        else
                        {
                            offDis = Vector2.Distance(touch1Delta, touch2Delta);
                        }
                        TowTouchScale(offDis * 0.01f);
                    }
                }
                _lastTwoDis = curDis;
                _lastTwoDir = curDir;
            }
            else
            {
                _lastTouchPos1 = t1.position;
                _lastTouchPos2 = t2.position;
            }
        }
    }

    private void TowTouchScale(float offDis)
    {
        float fov = gameObject.GetComponent<Camera>().fieldOfView;
        fov += offDis * 0.1f;
        if (fov < minFov)
        {
            fov = minFov;
        }
        if (fov > maxFov)
        {
            fov = maxFov;
        }
        gameObject.GetComponent<Camera>().fieldOfView = fov;
    }
  
    private void MouseScrollWheel() {
        if (Input.GetAxis("Mouse ScrollWheel") != 0)
        {
            float offset = Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * 500;
            //gameObject.transform.Translate(new Vector3(0, 0, Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * 500));
            float fov = gameObject.GetComponent<Camera>().fieldOfView;
            fov += offset;
            if (fov >= maxFov)
            {
                fov = maxFov;
            }
            if (fov <= minFov)
            {
                fov = minFov;
            }
            gameObject.GetComponent<Camera>().fieldOfView = fov;
        }  
    }     
  
}  